/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.Modem;
import br.com.jr2tecnologia.model.repository.ModemRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class ModemDAO extends GenericDAO<Modem, ModemRepository> implements ModemRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public Modem findById(Long id) {
        return (Modem)em.createNamedQuery("Modem.findByPkModem").
                setParameter("pkModem", id).getSingleResult();
    }

    public Modem findByName(String name) {
        try{
            return (Modem)em.createNamedQuery("Modem.findByName").
                setParameter("name", name).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public Modem findBySmscRoute(String smscRoute){
        try{
            return (Modem)em.createNamedQuery("Modem.findBySmscRoute").
                setParameter("smscRoute", smscRoute).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<Modem> list(Modem modem){
        try{
            Login login = modem.getLogin();
            Company company = modem.getCompany();
            String hql = "select modem from Modem as modem ";
            hql+=" left outer join modem.login as login";
            hql+=" left outer join modem.company as company";
            hql+=" where modem.pkModem = modem.pkModem";

            if(modem.getWorking() != null)
                hql+=" and modem.working = :modem_working";
            if(modem.getName() != null && !modem.getName().equals(""))
                hql+=" and modem.name like :modem_name";
            if(modem.getPhone() != null && !modem.getPhone().equals(""))
                hql+=" and modem.phone like :modem.phone";
            if(login != null && login.getPkLogin() != null)
                hql+=" and login.pkLogin = :pkLogin";
            if(company != null && company.getPkCompany() != null)
                hql+=" and company.pkCompany = :pkCompany";
            hql+=" order by modem.name";

            Query query = em.createQuery(hql);
            if(modem.getWorking() != null)
                query.setParameter("modem_working", modem.getWorking());
            if(modem.getName() != null && !modem.getName().equals(""))
                query.setParameter("modem_name", modem.getName()+"%");
            if(modem.getPhone() != null && !modem.getPhone().equals(""))
                query.setParameter("modem_phone", modem.getPhone()+"%");
            if(login != null && login.getPkLogin() != null)
                query.setParameter("pkLogin", login.getPkLogin());
            if(company != null && company.getPkCompany() != null)
                query.setParameter("pkCompany", company.getPkCompany());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list modem",e);
            throw new DAOException(e);
        }
    }

    public List<Modem> listAllSharedModems(){
        try{
            String hql = " select modem from Modem as modem";
            hql+= " where modem.company is null and modem.login is null";
            hql+=" and modem.working = :modem_working";
            hql+=" order by modem.name";

            Query query = em.createQuery(hql);
            query.setParameter("modem_working", true);
            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list modem",e);
            throw new DAOException(e);
        }
    }

    public List<Modem> listAll(){
        try{
            return em.createNamedQuery("Modem.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

}
